<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class Intercourse extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $keyword = $this->request->post('keyword');
            $types = $this->request->post('types');
            if(!empty($types)){
                $types = explode(',',$types);
            }

            $customers = [];
            if(empty($types) || in_array(1,$types)){
                $where = [];
                if(!empty($keyword)){
                    $where['customer_code|customer_name'] = ['like','%'.$keyword.'%'];
                }
                $customers = Db::name('kuyou_customers')->where($where)->select();
            }
            $suppliers = [];
            if(empty($types) || in_array(2,$types)){
                $where = [];
                if(!empty($keyword)){
                    $where['supplyer_code|supplyer_name'] = ['like','%'.$keyword.'%'];
                }
                $suppliers = Db::name('kuyou_suppliers')->where($where)->select();
            }

            $list = [];
            foreach ($customers as $customers_k=>$customers_v){
                $list[] = [
                    'code'      => $customers_v['customer_code'],
                    'name'      => $customers_v['customer_name'],
                    'type'      => 1,
                    'receive'   => $customers_v['need_receive_account'],
                    'give'      => ''
                ];
            }
            foreach ($suppliers as $suppliers_k=>$suppliers_v){
                $list[] = [
                    'code'      => $suppliers_v['supplyer_code'],
                    'name'      => $suppliers_v['supplyer_name'],
                    'type'      => 2,
                    'receive'   => '',
                    'give'      => $suppliers_v['need_give_account'],
                ];
            }

            $result = array("count" => sizeof($list), "list" => $list);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
